<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="时间筛选">
        <el-date-picker
          v-model="dataForm.time"
          type="daterange"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="timeOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.goodsName" placeholder="商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="店铺名称">
        <el-input v-model="dataForm.shopName" placeholder="店铺名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="记录类型">
        <el-select v-model="dataForm.type" clearable placeholder="全部">
          <el-option
            v-for="type in types"
            :key="type.value"
            :label="type.label"
            :value="type.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      :header-cell-style="{background:'#17b3a3',color:'#fff'}"
    >
      <el-table-column prop="shopName" header-align="center" align="center" label="店铺"></el-table-column>
      <!-- <el-table-column prop="depotId" header-align="center" align="center" label="仓库id"></el-table-column> -->
      <el-table-column prop="goodsName" header-align="center" align="center" label="商品名称"></el-table-column>
      <el-table-column prop="num" header-align="center" align="center" label="数量"></el-table-column>
      <!-- 1-出库；2-入库；4-销售 -->
      <el-table-column prop="typeStr" header-align="center" align="center" label="类型"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="时间">
        <template slot-scope="{row}">{{dateFormat(row.createTime, 'YYYY-MM-DD HH:mm:ss')}}</template>
      </el-table-column>
      <!-- <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        time: [],
        goodsName: "",
        shopName: "",
        type: null
      },
      timeOptions: {
        // 大于当前时间不可选
        disabledDate(time) {
          return (
            time.getTime() > new Date(new Date().toLocaleDateString()).getTime()
          );
        }
      },
      types: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  components: {},
  mounted() {
    this.getTypes();
  },
  activated() {
    this.getDataList();
  },
  methods: {
    init() {
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      let time = "";
      if (this.dataForm.time && this.dataForm.time.length == 2) {
        time = this.dataForm.time[0] + "~" + this.dataForm.time[1];
      }
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/merchant/shop/stock/record/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          time: time,
          goodsName: this.dataForm.goodsName,
          shopName: this.dataForm.shopName,
          type: this.dataForm.type
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 获取记录类型
    getTypes() {
      this.types = [];
      this.$http({
        url: this.$http.adornUrl("/merchant/shop/stock/record/type"),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data && data.code == 0) {
          this.types = data.data;
        }
      });
    }
  }
};
</script>